package com.ruoyi.ljj.service.impl;

import java.lang.reflect.GenericArrayType;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ljj.domain.GasoInfo;
import com.ruoyi.ljj.domain.Gasoline;
import com.ruoyi.ljj.mapper.GasolineMapper;
import com.ruoyi.ljj.service.IGasolineService;
import com.ruoyi.ljj.utils.IdGen;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 加注Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-14
 */
@Service
public class GasolineServiceImpl implements IGasolineService
{
    @Autowired
    private GasolineMapper gasolineMapper;

    /**
     * 查询加注
     * 
     * @param id 加注主键
     * @return 加注
     */
    @Override
    public Gasoline selectGasolineById(String id)
    {
        return gasolineMapper.selectGasolineById(id);
    }

    /**
     * 查询加注列表
     * 
     * @param gasoline 加注
     * @return 加注
     */
    @Override
    public List<Gasoline> selectGasolineList(Gasoline gasoline)
    {
        return gasolineMapper.selectGasolineList(gasoline);
    }

    /**
     * 新增加注
     * 
     * @param gasoline 加注
     * @return 结果
     */
    @Override
    public int insertGasoline(Gasoline gasoline)
    {
        Gasoline gasoline1 = autoFill(gasoline);
        gasoline1.setCreateTime(DateUtils.getNowDate());
        return gasolineMapper.insertGasoline(gasoline1);
    }

    /**
     * 补充信息
     * @param gasoline
     * @return
     */
    public Gasoline autoFill(Gasoline gasoline){
        //设置id
        gasoline.setId(IdGen.uuid());
        //设置数量
        BigDecimal totalFee = gasoline.getTotalFee();
        BigDecimal price = gasoline.getPrice();
        BigDecimal divide = totalFee.divide(price, 2, BigDecimal.ROUND_HALF_UP);
        String s = divide.toString();
        gasoline.setCount(s);
        //设置优惠单价及优惠总价/数量  每升优惠金额 及 优惠总金额
        if( 0 != gasoline.getTotalFee().compareTo(gasoline.getDiscountsTotalFee()) ){
            gasoline.setDiscountsPrice(gasoline.getDiscountsTotalFee().divide(new BigDecimal(s),2, BigDecimal.ROUND_HALF_UP));
            gasoline.setDiscountsIntervalPrice(gasoline.getPrice().subtract(gasoline.getDiscountsPrice()));
            gasoline.setDiscountsIntervalTotal(gasoline.getTotalFee().subtract(gasoline.getDiscountsTotalFee()));
        }else{
            gasoline.setDiscountsPrice(new BigDecimal(0));
            gasoline.setDiscountsIntervalPrice(new BigDecimal(0));
            gasoline.setDiscountsIntervalTotal(new BigDecimal(0));
        }
        //本次加注满
        if(gasoline.getFullStatus().equals("1")) {
            //查询上次加满的数据
            Gasoline gaso = new Gasoline();
            gaso.setFullStatus("1");
            gaso.setLicenseNum(gasoline.getLicenseNum());
            List<Gasoline> gasolines = gasolineMapper.selectGasolineList(gaso);
            if (!gasolines.isEmpty()) {
                Gasoline gasoline1 = gasolines.get(0);
                Date fillDate = gasoline1.getFillDate();
                //上次加注时间
                gasoline.setKmBeforeTime(fillDate);
                //间隔时间
                long intervalTime = gasoline.getFillDate().getTime() - fillDate.getTime();
                long day = 0;
                if (intervalTime >= 24 * 60 * 60 * 1000) {
                    day = intervalTime / (24 * 60 * 60 * 1000);
                }
                String s1 = String.valueOf(day);
                gasoline.setFillIntervalTime(s1);
                //距离上次公里数
                Long aLong = Long.valueOf(gasoline.getKm());
                Long aLong1 = Long.valueOf(gasoline1.getKm());
                Long intervalKm = aLong - aLong1;
                gasoline.setKmInterval(String.valueOf(intervalKm));
                //每公里金额,每公里优惠之后金额及百公里注耗
                Gasoline gakf = new Gasoline();
                gakf.setLicenseNum(gasoline.getLicenseNum());
                gakf.setFillDate(gasoline1.getFillDate());
                gakf.setFullStatus("0");
                List<Gasoline> gasolines1 = gasolineMapper.selectGasolineByKF(gakf);
                BigDecimal totalFee1 = new BigDecimal("0");
                BigDecimal totalFee2 = new BigDecimal("0");
                Double fullc = 0.00;
                for (Gasoline gasoline2 : gasolines1) {
                    BigDecimal discountsTotalFee = gasoline2.getDiscountsTotalFee();
                    BigDecimal TotalFee = gasoline2.getTotalFee();
                    totalFee1 = totalFee1.add(discountsTotalFee);
                    totalFee2 = totalFee2.add(TotalFee);
                    fullc = fullc + Double.valueOf(gasoline2.getCount());
                }
                //注耗
                fullc = fullc + Double.valueOf(gasoline.getCount());
                BigDecimal fullcc = new BigDecimal(fullc);
                BigDecimal fullc1 = new BigDecimal(gasoline.getKmInterval());
                BigDecimal divide2 = fullcc.divide(fullc1, 4, BigDecimal.ROUND_HALF_UP);
                BigDecimal multiply = divide2.multiply(new BigDecimal(100));
                //bigdecimal保留两位小数
                BigDecimal bgD = multiply.setScale(2, BigDecimal.ROUND_HALF_UP);
                gasoline.setKmConsume(bgD.toString());
                //金额
                totalFee1 = gasoline.getDiscountsTotalFee();
                totalFee2 = gasoline.getTotalFee();
                BigDecimal kmp = totalFee1.divide(new BigDecimal(gasoline.getKmInterval()), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal kmp2 = totalFee2.divide(new BigDecimal(gasoline.getKmInterval()), 2, BigDecimal.ROUND_HALF_UP);
                gasoline.setDiscountsKmPrice(kmp);
                gasoline.setKmPrice(kmp2);
            }
            return gasoline;
        }else{
            //查询上次加满的数据
            Gasoline gaso2 = new Gasoline();
            gaso2.setLicenseNum(gasoline.getLicenseNum());
            List<Gasoline> gasolines2 = gasolineMapper.selectGasolineList(gaso2);
            if (!gasolines2.isEmpty()) {
                Gasoline gasoline2 = gasolines2.get(0);
                Date fillDate = gasoline2.getFillDate();
                //上次加注时间
                gasoline.setKmBeforeTime(fillDate);
                //间隔时间
                long intervalTime = gasoline.getFillDate().getTime() - fillDate.getTime();
                long day = 0;
                if (intervalTime >= 24 * 60 * 60 * 1000) {
                    day = intervalTime / (24 * 60 * 60 * 1000);
                }
                String s1 = String.valueOf(day);
                gasoline.setFillIntervalTime(s1);
                //距离上次公里数
                Long aLong = Long.valueOf(gasoline.getKm());
                Long aLong1 = Long.valueOf(gasoline2.getKm());
                Long intervalKm = aLong - aLong1;
                gasoline.setKmInterval(String.valueOf(intervalKm));
                return gasoline;
            }
            return gasoline;
        }

    }

    /**
     * 修改加注
     * 
     * @param gasoline 加注
     * @return 结果
     */
    @Override
    public int updateGasoline(Gasoline gasoline)
    {
        gasoline.setUpdateTime(DateUtils.getNowDate());
        return gasolineMapper.updateGasoline(gasoline);
    }

    /**
     * 批量删除加注
     * 
     * @param ids 需要删除的加注主键
     * @return 结果
     */
    @Override
    public int deleteGasolineByIds(String[] ids)
    {
        return gasolineMapper.deleteGasolineByIds(ids);
    }

    /**
     * 删除加注信息
     * 
     * @param id 加注主键
     * @return 结果
     */
    @Override
    public int deleteGasolineById(String id)
    {
        return gasolineMapper.deleteGasolineById(id);
    }

    @Override
    public List<GasoInfo> getGasoInfo(GasoInfo gasoInfo){
        List<GasoInfo> arr = new ArrayList<>();
        BigDecimal dinterprice = gasoInfo.getDinterprice();
        BigDecimal price = gasoInfo.getPrice();
        //优惠单价差额
        if(0 != dinterprice.compareTo(price)){
            gasoInfo.setIntervprice(gasoInfo.getPrice().subtract(gasoInfo.getDinterprice()));
        }else{
            gasoInfo.setIntervprice(new BigDecimal(0));
        }
        //实际总价
        gasoInfo.setTotalfeey(gasoInfo.getPrice().multiply(new BigDecimal("35")));
        //优惠总价
        gasoInfo.setDistotalfeey(gasoInfo.getDinterprice().multiply(new BigDecimal("35")));
        //总价差额
        gasoInfo.setNtertotalfeey(gasoInfo.getTotalfeey().subtract(gasoInfo.getDistotalfeey()));
        //预计剩余量
        Gasoline gasoline = new Gasoline();
        gasoline.setLicenseNum(gasoInfo.getLicensenum());
        gasoline.setFullStatus("1");
        List<Gasoline> gasolines = gasolineMapper.selectGasolineList(gasoline);
        if(gasolines.isEmpty()) return arr;
        //上次注满
        Gasoline gasoline1 = gasolines.get(0);
        Gasoline pms = new Gasoline();
        pms.setFillDate(gasoline1.getFillDate());
        pms.setLicenseNum(gasoline1.getLicenseNum());
        pms.setFullStatus("0");
        List<Gasoline> gasolines1 = gasolineMapper.selectGasolineByKF(pms);
        //中间加注量
        Double count = 0.0;
        for (Gasoline gasoline2 : gasolines1) {
            count = count + Double.valueOf(gasoline2.getCount());
        }
        //km
        BigDecimal kminter = new BigDecimal(gasoInfo.getKm()).subtract(new BigDecimal(gasoline1.getKm()));
        BigDecimal kmCont = new BigDecimal(gasoline1.getKmConsume());
        //耗量
        BigDecimal divide = (kminter.multiply(kmCont)).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal(count));
        //剩量
        gasoInfo.setRevo(new BigDecimal("35").subtract(divide).toString());
        //预计量
        gasoInfo.setPlvo(divide.toString());
        //剩余警告里程
        String rek;
        switch(Integer.valueOf(gasoInfo.getWarny())){
            case 0:
                rek = new BigDecimal(gasoInfo.getRevo()).subtract(new BigDecimal("10.5")).toString();
                break;
            case 1:
                rek = new BigDecimal(gasoInfo.getRevo()).subtract(new BigDecimal("3.5")).toString();
                break;
            case 2:
                rek = new BigDecimal(gasoInfo.getRevo()).toString();
                break;
            default:
                rek = new BigDecimal(gasoInfo.getRevo()).toString();
                break;
        }
        gasoInfo.setRekm(new BigDecimal(rek).divide(new BigDecimal(gasoline1.getKmConsume()).divide(new BigDecimal("100")),2,BigDecimal.ROUND_HALF_UP).toString());
        //预计行驶警告里程
        gasoInfo.setPlkm(new BigDecimal(gasoInfo.getKm()).add(new BigDecimal(gasoInfo.getRekm())).toString());
        //预计金额
        gasoInfo.setPlfee(divide.multiply(gasoInfo.getPrice()).setScale(2,BigDecimal.ROUND_HALF_UP).toString());
        //预计总里程
        gasoInfo.setTotalkmy(new BigDecimal("35").divide((new BigDecimal(gasoline1.getKmConsume()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_HALF_UP)),2,BigDecimal.ROUND_HALF_UP).toString());
        //总容积
        gasoInfo.setTotalvoy("35");
        BigDecimal revode = new BigDecimal(gasoInfo.getRevo());
        BigDecimal ye = new BigDecimal(gasoInfo.getTotalvoy()).multiply(new BigDecimal("0.3"));
        BigDecimal re = new BigDecimal(gasoInfo.getTotalvoy()).multiply(new BigDecimal("0.1"));
        if(0 < revode.compareTo(re)){
            if(0 < revode.compareTo(ye)){
                gasoInfo.setWarny("0");
            }else{
                gasoInfo.setWarny("1");
            }
        }else {
            gasoInfo.setWarny("2");
        }
        //剩余百分比
        String s = myPercent(Double.valueOf(revode.toString()),Double.valueOf(35));
        gasoInfo.setBfvoy(s);
        arr.add(gasoInfo);
        return arr;
    }

    /**
     * 百分比
     * @param y
     * @param z
     * @return
     */
    public static String myPercent(Double y, Double z) {
        String baifenbi = "";// 接受百分比的值
        double baiy = y * 1.0;
        double baiz = z * 1.0;
        double fen = baiy / baiz;
        // NumberFormat nf = NumberFormat.getPercentInstance();注释掉的也是一种方法
        // nf.setMinimumFractionDigits( 2 ); 保留到小数点后几位
        DecimalFormat df1 = new DecimalFormat("##.00%");
        // ##.00%
        // 百分比格式，后面不足2位的用0补齐
        // baifenbi=nf.format(fen);
        baifenbi = df1.format(fen);
        System.out.println(baifenbi);
        return baifenbi;
    }

    public static void main(String[] args) {
        String l = "5.5";
        String danjia = "7.18";
        String kmp = new BigDecimal(danjia).multiply(new BigDecimal(l)).divide(new BigDecimal("100"),2,BigDecimal.ROUND_HALF_UP).toString();
        String yunkm = (new BigDecimal("100").divide(new BigDecimal(danjia),2,BigDecimal.ROUND_HALF_UP)).divide(new BigDecimal(l).divide(new BigDecimal("100")),2, BigDecimal.ROUND_HALF_UP).toString();
        System.out.println("每公里单价"+kmp);
        System.out.println("每百元公里"+yunkm);
    }
}
